<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.List"%>
<%@page import="org.nado.bbs.BBSBoardVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
	request.setCharacterEncoding("UTF-8");
%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>BBS</title>
		<meta name="author" content="bit1" />
<base href="/resources/" />
<script type="text/javascript">
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch (e) {
	}
</script>

<!-- 스타일 시트 -->
<link rel="stylesheet" type="text/css" href="css/main.css">
<link rel="stylesheet" type="text/css" href="css/bbs.css">
<style>

</style>

</head>
<body onload="eventonload()" style="overflow-y: hidden">

<div class="main">
	<div id="main_bg">
	<h3 style="font-family:'Denk One'; font-size:40pt; margin-bottom:10px; text-shadow: rgba(0, 0, 0, 0.5) 0 8px 8px;">BBS</h3>
	<table>
		<tr>
			<th>No</th>
			<th>Title</th>
			<th>Writer</th>
			<th>RegDate</th>
			<th>Count</th>
		</tr>
		
<!-- 		1. EL Style 리스트 출력 -->

<%-- 		<c:forEach var="BBSList" items="${BBSList}"> --%>
<%-- 			<td>${BBSList.bbs_no}</td> --%>
<!-- 			<td style="text-align: left"> -->
<%-- 				<a href="/bbs/read?bbs_no=${BBSList.bbs_no}&page_no=${page_no}" style="text-decoration: none">  --%>
<%-- 				<c:if test="${BBSList.bbs_depth != '0'}"> --%>
<%-- 					<c:forEach begin="1" end="${BBSList.bbs_depth}"> --%>
<!-- 										Re: -->
<!-- 					┗ -->
<%-- 					</c:forEach> --%>
<%-- 				</c:if> ${BBSList.bbs_title} (${BBSList.bbs_recnt}) --%>
<!-- 				</a></td> -->
<%-- 			<td>${BBSList.bbs_writer}</td> --%>
<%-- 			<td><c:set var="date" value="${BBSList.bbs_regDate}" />  --%>
<%-- 			<fmt:formatDate	value="${date}" pattern="yyyy/MM/dd" /></td> --%>
<%-- 			<td>${BBSList.bbs_readcnt}</td> --%>
<%-- 		</c:forEach> --%>

		<%
		// 2. java style 리스트 출력
		
		int page_no = (Integer) request.getAttribute("page_no");
		List<BBSBoardVO> list = (List<BBSBoardVO>) request.getAttribute("BBSList");
		for(BBSBoardVO vo: list){
			%>
		<tr>
			<td><%=vo.getBbs_no()%></td>
			<td><a href="/bbs/read?bbs_no=<%=vo.getBbs_no()%>&page_no=<%=page_no%>" 
			style="text-decoration: none">
			<%
			if(vo.getBbs_depth() != 0){
				for(int i = 0; i < vo.getBbs_depth(); i++){
					out.write("┗");
				}
			}
			%>
			<%=vo.getBbs_title()%></a></td>
			<td><%=vo.getBbs_writer()%></td>
			<%
			SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
			%>
			<td><%=format.format(vo.getBbs_regDate())%></td>
			<td><%=vo.getBbs_readcnt()%></td>
		</tr>
			<%
		}
		%>
		<tr>
		<td style="text-align: left" colspan="3"><br></br>
			<form action="/bbs/search/page_no=1" method="post">
			<select name="type">
				<option value="bbs_title">제목</option>
				<option value="bbs_content">내용</option>
				<option value="bbs_writer">작성자</option>
			</select>
			<input type="text" name="keyword" size="20" required="required"></input>
			<input type="submit" value="검색"></input>
			</form></td>
			
		<td style="text-align: right" colspan="2">
			<br></br>
			
			<%
			// 리스트 페이징
			int initPage = -1;
			int maxPage = -1;
// 			boolean hasNext = false;
// 			List<BBSBoardVO> list = (List<BBSBoardVO>) request.getAttribute("BBSList");
// 			int page_no = (Integer) request.getAttribute("page_no");

			maxPage = (int) Math.ceil(list.get(0).getBbs_totalcnt()/(double)10); 
			initPage = (int) (Math.ceil(page_no/(double)10) *10) -9;
		
			System.out.println("maxPage: " + maxPage);
			System.out.println("initPage: " + initPage);
			
			// maxPage가 1이 아니고 11 이상의 로직 처리
// 			if(maxPage != 1  && (maxPage % 10 >= 1) ){
// 				hasNext = true;
// 				System.out.println("hasNext: "+hasNext);
// 			}
			if(initPage != 1){
				%>
				<a href="/bbs/list?page_no=<%=initPage - 1 %>" 
				style="text-decoration: none"> << </a>
				<%
			}
			for(int i = initPage; i <= maxPage; i++){
				if(i == (initPage+10)){
// 					if(hasNext == true){
						%>
						<a href="/bbs/list?page_no=<%=initPage + 10 %>" 
						style="text-decoration: none"> >> </a>
						<%
// 					}
					break;
				}else{
					if(i == page_no){
						out.write("["+i+"]");
					}else{
					%>
					<a href="/bbs/list?page_no=<%=i %>" 
					style="text-decoration: none">[<%=i %>] </a>
					<%
					}
				}
			}
			%>
			<form action="/bbs/write" method="post">
			<input type="submit" name="write" value="글쓰기"></input>
			</form>
		</td>
		</tr>
	</table>
	</div>
</div>
</body>
</html>